package org.jeecg.modules.workflow.controller.task;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.task.task.*;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.service.task.BpmTaskService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "管理后台 - 流程任务实例")
@RestController
@RequestMapping("/bpm/task")
@Validated
public class BpmTaskController extends BaseController {

    @Resource
    private BpmTaskService taskService;

    @GetMapping("todo-page")
    @ApiOperation("获取 Todo 待办任务分页")
    //@RequiresPermissions("bpm:task:query")
    public Result<IPage<BpmTaskTodoPageItemRespVO>> getTodoTaskPage(@Valid BpmTaskTodoPageReqVO pageVO) {
        IPage<BpmTaskTodoPageItemRespVO> pageResult = taskService.getTodoTaskPage(getUserId(), pageVO);
        return Result.OK(pageResult);
    }

    @GetMapping("todo-page/{processName}")
    @ApiOperation("根据流程定义名称分类获取 Todo 待办任务分页")
    //@RequiresPermissions("bpm:task:query")
    public Result<IPage<BpmTaskTodoPageItemRespVO>> getTodoTaskPageByDefId(@Valid BpmTaskTodoPageReqVO pageVO, @PathVariable String processName) {
        IPage<BpmTaskTodoPageItemRespVO> pageResult = taskService.getTodoTaskPage(getUserId(), processName, pageVO);
        return Result.OK(pageResult);
    }

    @GetMapping("done-page")
    @ApiOperation("获取 Done 已办任务分页")
    //@RequiresPermissions("bpm:task:query")
    public Result<IPage<BpmTaskDonePageItemRespVO>> getTodoTaskPage(@Valid BpmTaskDonePageReqVO pageVO) {
        IPage<BpmTaskDonePageItemRespVO> pageResult = taskService.getDoneTaskPage(getUserId(), pageVO);
        return Result.OK(pageResult);
    }

    @PutMapping("/approve")
    @ApiOperation("通过任务")
    //@RequiresPermissions("bpm:task:update")
    public Result<Boolean> approveTask(@Valid @RequestBody BpmTaskApproveReqVO reqVO) {
        taskService.approveTask(getUserId(), reqVO);
        return Result.OK(true);
    }

    @PutMapping("/reject")
    @ApiOperation("不通过任务")
    //@RequiresPermissions("bpm:task:update")
    public Result<Boolean> rejectTask(@Valid @RequestBody BpmTaskRejectReqVO reqVO) {
        taskService.rejectTask(getUserId(), reqVO);
        return Result.OK(true);
    }

    @PutMapping("/update-assignee")
    @ApiOperation(value = "更新任务的负责人", notes = "用于【流程详情】的【转派】按钮")
    //@RequiresPermissions("bpm:task:update")
    public Result<Boolean> updateTaskAssignee(@Valid @RequestBody BpmTaskUpdateAssigneeReqVO reqVO) {
        taskService.updateTaskAssignee(getUserId(), reqVO);
        return Result.OK(true);
    }

    @GetMapping("/list-by-process-instance-id")
    @ApiOperation(value = "获得指定流程实例的任务列表", notes = "包括完成的、未完成的")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    //@RequiresPermissions("bpm:task:query")
    public Result<List<BpmTaskRespVO>> getTaskListByProcessInstanceId(
            @RequestParam("processInstanceId") String processInstanceId) {
        return Result.OK(taskService.getTaskListByProcessInstanceId(processInstanceId));
    }

    private String getUserId()
    {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getId();
    }

    @PutMapping("/flowTackback")
    @ApiOperation("驳回任务")
    //@PreAuthorize("@ss.hasPermi('bpm:task:flowTackback')")
    public Result<Boolean> flowTackback(@Valid @RequestBody BpmTaskApproveReqVO reqVO) {
        taskService.flowTackback(getUserId(), reqVO);
        return Result.OK(true);
    }


    /**
     * 获取所有可退回的节点
     * @param taskId
     * @return
     */
    @GetMapping(value = "/findReturnTasks")
    @ApiOperation("获取所有可退回的节点")
    //@PreAuthorize("@ss.hasPermi('bpm:task:findReturnTasks')")
    public Result<List<BpmTaskRespVO>> findReturnTask(@RequestParam("taskId") String taskId) {
        List<BpmTaskRespVO> bpmTaskRespVOS = taskService.findReturnTasks(taskId);
        return Result.OK(bpmTaskRespVOS);
    }

    /**
     * 流程回退
     * @param reqVO
     * @return
     */
    @PutMapping("/flowReturn")
    @ApiOperation("流程回退")
    //@PreAuthorize("@ss.hasPermi('bpm:task:flowReturn')")
    public Result<Boolean> flowReturn(@Valid @RequestBody BpmTaskBackReqVO reqVO) {
        taskService.flowReturn(getUserId(), reqVO);
        return Result.OK(true);
    }

    @GetMapping("/getTaskDetail")
    @ApiOperation(value = "获得task详情")
    @ApiImplicitParam(name = "taskId", value = "taskId", required = true, dataTypeClass = String.class)
    public Result<BpmTaskRespVO> getTaskDetailById(
            @RequestParam("taskId") String taskId) {
        return Result.OK(taskService.getTaskDetailById(getUserId(), taskId));
    }

}
